package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.feed.FeedAllocConfigDTO;
import com.qkl.xiatang.app.dto.feed.FeedVO;
import com.qkl.xiatang.app.dto.message.MessageSendVO;
import com.qkl.xiatang.app.dto.pond.QueryPondNamesOutDTO;
import com.qkl.xiatang.app.entity.feed.FeedAllocConfig;
import com.qkl.xiatang.app.mapper.FeedAllocConfigMapper;
import com.qkl.xiatang.app.mapper.FeedAllocMapper;
import com.qkl.xiatang.app.service.config.FeedAllocConfigService;
import com.qkl.xiatang.app.utils.AppDateUtils;
import com.qkl.xiatang.app.utils.AppUtils;
import com.qkl.xiatang.app.utils.appenum.MessageTypeEnum;
import com.qkl.xiatang.app.utils.appenum.MessageTypeSubEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 喂料提醒通知
 */
@Service
@Slf4j
public class FeedNoticeService {

    @Autowired
    private FeedAllocConfigMapper feedAllocConfigMapper;

    @Autowired
    private FeedAllocMapper feedAllocMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private PondNamesService pondNamesService;


    @Autowired
    private FeedManagerService feedManagerService;

    @Autowired
    private MessageDetailService messageDetailService;

    @Autowired
    private FeedAllocConfigService feedAllocConfigService;


    /**
     * 未喂料提醒通知
     * @param queryCycleMinute 隔多少分钟查谒
     */
    public void  unfeededNotice(int queryCycleMinute){
        Calendar calendar2 = Calendar.getInstance();
        int nowHour = calendar2.get(Calendar.HOUR_OF_DAY);
        long nowTime = System.currentTimeMillis();
        List<FeedAllocConfigDTO> unfeedFeedConfigList = new ArrayList<>();//未喂料
        List<FeedAllocConfigDTO> uncheckConfigList = new ArrayList<>();//未查料台
        List<FeedAllocConfigDTO> unfeedZooConfigList = new ArrayList<>();//未投动保

        List<FeedAllocConfigDTO> configList = this.queryConfigList();
        if(ObjectUtils.isNotEmpty(configList)){
            for(FeedAllocConfigDTO config : configList){
                Date endTime = config.getEndTime();//喂料结束时间
                Date endTimeCheck = AppDateUtils.addHours(endTime,1);//查料台结束时间
                if(AppDateUtils.betweenEndDate(nowTime,endTime,queryCycleMinute)){
                    unfeedFeedConfigList.add(config);
                }
                if(AppDateUtils.betweenEndDate(nowTime,endTimeCheck,queryCycleMinute)){
                    uncheckConfigList.add(config);
                }
            }

        }
        if(AppDateUtils.betweenEndHour(nowHour,nowTime,20,queryCycleMinute)){
            unfeedZooConfigList.addAll(this.queryZooConfigList());
        }

        sendUnfeedFeedMessage(unfeedFeedConfigList);
        sendUncheckMessage(uncheckConfigList);
        sendUnfeedZooMessage(unfeedZooConfigList);

    }

    public void testFeedNotice(){
        List<FeedAllocConfigDTO> configList = this.queryConfigList();
        if(ObjectUtils.isEmpty(configList)){
            return;
        }
        sendUnfeedFeedMessage(configList);
        sendUncheckMessage(configList);
        sendUnfeedZooMessage(this.queryZooConfigList());
        //sendNotAllocMessage(configList);
    }

    private void sendUnfeedFeedMessage(List<FeedAllocConfigDTO> unfeedFeedConfigList){
        if(ObjectUtils.isEmpty(unfeedFeedConfigList)){
            return;
        }
        for(FeedAllocConfigDTO config : unfeedFeedConfigList){
            Date allocDate = config.getAllocDate();
            List<QueryPondNamesOutDTO> pondNamesList = pondNamesService.queryUnfeededFeedPondNames(config.getCompanyId(),allocDate,config.getSerialNo());
            if(ObjectUtils.isEmpty(pondNamesList)){
                continue;
            }
            log.info("================= start 饲料未投放");
            String companyId = config.getCompanyId();
            MessageTypeEnum typeEnum = MessageTypeEnum.ADMIN;
            int subType = MessageTypeSubEnum.FEED_NOT_FEED.getSubType();
            FeedVO feedVO = messageDetailService.queryFeedVoPondNames(pondNamesList);
            String title = MessageTypeSubEnum.FEED_NOT_FEED.getName();
            String titleSub = AppDateUtils.formatMsgDate(allocDate) + config.getSerialName();// + MsgUtils.formatMessage(feedVO.getNameList())
            String content = QueryPondNamesOutDTO.toMessage(pondNamesList,feedVO);
            MessageSendVO sendVO = MessageSendVO.create(companyId,title,titleSub,typeEnum,subType,allocDate,content);
            sendVO.addDetailList(pondNamesList,feedVO);
            sendVO.setImageList(feedVO.getImageList());
            sendVO.setMessage(this.getTemplateName(companyId,config.getTemplateId()));
            messageService.sendMessage(sendVO);
            log.info("================= end 饲料未投放");
        }
    }

    private void sendUncheckMessage(List<FeedAllocConfigDTO> uncheckConfigList){
        if(ObjectUtils.isEmpty(uncheckConfigList)){
            return;
        }
        for(FeedAllocConfigDTO config : uncheckConfigList){
            Date allocDate = config.getAllocDate();
            List<QueryPondNamesOutDTO> pondNamesList = pondNamesService.queryUncheckedPondNames(config.getCompanyId(),allocDate,config.getSerialNo());
            if(ObjectUtils.isEmpty(pondNamesList)){
                continue;
            }
            String companyId = config.getCompanyId();
            MessageTypeEnum typeEnum = MessageTypeEnum.ADMIN;
            int subType = MessageTypeSubEnum.CHECK_UNCHECK.getSubType();

            FeedVO feedVO = messageDetailService.queryFeedVoPondNames(pondNamesList);
            String title = MessageTypeSubEnum.CHECK_UNCHECK.getName();
            String titleSub = AppDateUtils.formatMsgDate(allocDate) + config.getSerialName();
            String content = QueryPondNamesOutDTO.toMessage(pondNamesList,feedVO);
            MessageSendVO sendVO = MessageSendVO.create(companyId,title,titleSub,typeEnum,subType,allocDate,content);
            sendVO.addDetailList(pondNamesList,feedVO);
            sendVO.setImageList(feedVO.getImageList());
            sendVO.setMessage(this.getTemplateName(companyId,config.getTemplateId()));
            messageService.sendMessage(sendVO);
        }
    }

    private void sendUnfeedZooMessage(List<FeedAllocConfigDTO> unfeedZooConfigList){
        if(ObjectUtils.isEmpty(unfeedZooConfigList)){
            return;
        }
        for(FeedAllocConfigDTO config : unfeedZooConfigList){
            Date allocDate = config.getAllocDate();
            String companyId = config.getCompanyId();
            List<QueryPondNamesOutDTO> pondNamesList = pondNamesService.queryUnfeededZooPondNames(companyId,allocDate);
            if(ObjectUtils.isEmpty(pondNamesList)){
                continue;
            }
            int subType = MessageTypeSubEnum.ZOO_NOT_FEED.getSubType();
            MessageTypeEnum typeEnum = MessageTypeEnum.ADMIN;
            FeedVO feedVO = messageDetailService.queryFeedVoPondNames(pondNamesList);
            String title = MessageTypeSubEnum.ZOO_NOT_FEED.getName();
            String titleSub = AppDateUtils.formatMsgDate(allocDate);// + MsgUtils.formatMessage(feedVO.getNameList())
            String content = QueryPondNamesOutDTO.toMessage(pondNamesList,feedVO);
            MessageSendVO sendVO = MessageSendVO.create(companyId,title,titleSub,typeEnum,subType,allocDate,content);
            sendVO.addDetailList(pondNamesList,feedVO);
            sendVO.setImageList(feedVO.getImageList());
            messageService.sendMessage(sendVO);
        }
    }


    private List<FeedAllocConfigDTO> queryConfigList(){
        return feedAllocConfigService.queryFeedAllocConfig();
    }

    private List<FeedAllocConfigDTO> queryZooConfigList(){
        return feedAllocConfigService.queryZooAllocConfig();
    }

    private String getTemplateName(String companyId,String templateId){
        return feedAllocConfigService.getTemplateName(companyId,templateId);
    }


}
